Quaternions
Preface There seems to be a great deal of confusion regarding this topic amongst game developers, seemingly more so than any other topic in this field. The confusion manifests itself everywhere from commercial engine code, to newsgroup and forum posts, to a number of books geared towards game developers, and is further perpetuated by these sources. This article attempts to present quaternions as an intuitive and useful tool, as opposed to some nebulously defined magic box. Complex numbers One, although certainly not the only, way to motivate the definition of quaternions is through complex numbers. Note that this introduction is by no means mathematically rigorous; a number of shortcuts were taken in the interest of expedience. With that in mind, a complex number z is defined as z = x + \mathbf{i} y , where x and y are real numbers, and \mathbf{i}^2 = -1 . It is tempting to view z as a two-dimensional vector, with x and y viewed as coordinates; then we can identify each complex number x+\mathbf{i}y with the two-dimensional vector (x,y) . This allows us to immediately define addition and subtraction: (a,b)+(c,d)=(a+c,b+d) \Longrightarrow a+\mathbf{i}b + c+\mathbf{i}d = (a+c)+\mathbf{i}(b+d) , with subtraction defined similarly. That is, addition and subtraction are defined as if \mathbf{i} were any other variable. Continuing with the vector analogy for the time being, we switch from Cartesian coordinates to polar coordinates. Given a vector v = (x,y) , we can rewrite it as v = |v| (cos \theta, sin \theta) , where \theta is the angle between (x,y) and the horizontal axis, and |v| is the magnitude of the vector v , written |v| = \sqrt{x^2+y^2} . The angle \theta is simply \tan^{-1}\frac{y}{x} . Analogously, given a complex number z = x + \mathbf{i}y , we can rewrite it as z = |z| (cos \theta + \mathbf{i} sin \theta) . Here, \theta is the angle between x + \mathbf{i}y and the horizontal axis, and |z| is the complex modulus of the number z , written |z| = \sqrt{x^2+y^2} . The angle \theta is defined as in the vector case, and is called the argument of z . The bracketed term runs over the unit circle as \theta runs from 0 to 2\pi , which suggests that we might be able to define 2d rotations with a single complex number. Moving on to multiplication, we can, once again, treat \mathbf{i} as any variable, and expand (a+\mathbf{i}b)(c+\mathbf{i}d) as ac+a\mathbf{i}d+b\mathbf{i}c+\mathbf{i}^2bd = (ac-bd)+\mathbf{i}(ad+bc) . To define division, we let \frac{a+\mathbf{i} b}{c + \mathbf{i}d} = x+\mathbf{i}y , or a+\mathbf{i}b = (x + \mathbf{i}y)(c + \mathbf{i}d) = (cx-yd) + \mathbf{i}(xd+yc) , and solve the resulting system of two equations for x and y , yielding x=\frac{ac + bd}{c^2 + d^2}, y =\frac{bc - ad}{c^2 + d^2} , and therefore \frac{a+\mathbf{i} b}{c + \mathbf{i}d} = \frac{(ac + bd)+\mathbf{i}(bc - ad)}{c^2+d^2} . Note that c^2+d^2 is simply the squared modulus of the denominator c+\mathbf{i}d . If the modulus of the denominator were equal to one, the squared modulus would equal to one as well, and the fraction \frac{a+\mathbf{i} b}{c + \mathbf{i}d} , or, to employ different notation, (a+\mathbf{i} b) (c + \mathbf{i}d)^{-1} , would equal simply (ac + bd)+\mathbf{i}(bc - ad) . Note the similarity to the multiplication formula. Now suppose a^2+b^2=1 as well; then the modulus of the quotient, (ac+bd)^2+(bc-ad)^2 = (a^2 c^2 + b^2 d^2 + 2abcd) + (b^2 c^2 + a^2 d^2 - 2abcd) = a^2 c^2 + b^2 d^2 + b^2 c^2 + a^2 d^2 = a^2(c^2+d^2) + b^2(c^2+d^2) = a^2 + b^2 = 1 , as well. Similarly, we can show that the product of two complex numbers with the modulus of one, results in another complex number with the modulus of one. This further supports the intuition that complex numbers might describe rotations, since the modulus is simply the distance from the origin -- that is, multiplying two numbers on the unit circle results in another number on the unit circle. If complex numbers can represent rotations, we're likely going to need inverse rotations, as well. That is, given a complex number z , we wish to find the number z^{-1} , such that z\cdot z^{-1} = 1 . Let z=a+\mathbf{i}b , and z^{-1} = c+\mathbf{i}d . Then their product is (ac-bd)+\mathbf{i}(ad+bc) . If this is to equal 1 , a real number, the imaginary component ad+bc must equal zero, and the real component ac-bd must equal one. Solving the resulting system of equations for c and d , we obtain c = \frac{a}{a^2+b^2} and d = -\frac{b}{a^2+b^2} . Alternatively, we can write z^{-1}=\frac{a-\mathbf{i}b}{|z|^2} . The numerator a-\mathbf{i}b is called the conjugate of z . It is clear that if z is unit length, then its conjugate is its inverse.